<%@ page contentType="text/html;charset=UTF-8"%>
<%@ include file="/WEB-INF/views/include/taglib.jsp"%>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
	<title>创建报价单</title>
    <%@ include file="/WEB-INF/views/include/head.jsp"%>
    <script src="${ctxStatic}/js/jquery-1.9.1.min.js"></script>
    <script src="${ctxStatic}/js/DateTimePicker.js"></script>
    <script src="${ctxStatic}/js/style.js"></script>
    <link rel="stylesheet" href="${ctxStatic}/css/common_erp.css">
    <link rel="stylesheet" href="${ctxStatic}/css/layerSm.css">
    <script>
    // 查存标记 
        var existFlag; 
        $(document).ready(function(){
        	onClickTd();
        //下拉菜单绑定样式 
        $("p>select").select2();
    	$("#okButton").click(function(){
    			//检查凭证编号是否已入力 
    			if ($("#voucherNo").val().trim() == "") {
    				$("#voucherNo").addClass("error");
    				tipParent("请输入有效的凭证编号");
    				$("#voucherNo").val("");
					return;
				}else {
					if (existFlag == "false") {
						tipParent("编号已存在");
						return;
					}
				}
    			//下拉菜单
    			var nulStrFlag = "false";
    			var selectLength = $("tbody select").length;
    			if (selectLength == 0) {
    				tipParent("请选择业务结算");
					return;
				}
        		for (var i = 0; i < selectLength; i++) {
        			if ($($("tbody select")[i]).val() == "") {
        				$($("tbody select")[i]).addClass("select_red");
        				nulStrFlag = "true";
					}
				}
        		if (nulStrFlag == "true") {
        			tipParent("请选择会计科目");
					return;
				}
        		/* if ($("#filePath").val() == "" && $("#uploadFileName").val() == "") {
        			tipParent("请上传单据");
					return;
				} */
	    		//$("#showHtml").addClass("showHtml"); 
	    		setThisIndex();
	    		$("select").attr("disabled",true);
	    		$("textarea").attr("disabled",true);
	    		$("td p[class='tab-no']").html("");
	    		$("#okHead").show();
	    		$("#createHead").hide(); 
     	});
    });
    
    <!-- 设置下标 -->
	var tempIndex;
    function setThisIndex(){
        var reg=/tempIndex/g;
        var tableFit;
		var trLength = $("#listHtml tr").length;
		if (trLength >= 1) {
			for (var i = 0; i < trLength; i++) {
				tableFit = $("#listHtml tr").eq(i);
				$(tableFit).html($(tableFit).html().replace(reg,i));
			}
		}
	}
    <!-- 计算总金额 -->
    function sumAmount(){
    	var trLength = $("#listHtml tr").length;
    	var debitSumAmount = 0;
    	var creditSumAmount = 0;
    	var thisdebitAmount = 0;
    	var thiscreditAmount = 0;
    	var sumAmount = 0;
    	if (trLength >= 1) {
	    	for (var i = 0; i < trLength; i++) {
	    		thisdebitAmount = $("#listHtml tr").eq(i).find(".debitAmount").val().replace(",","");
	    		thiscreditAmount = $("#listHtml tr").eq(i).find(".creditAmount").val().replace(",","");
	    		
	    		if(thisdebitAmount != ""){
	    			debitSumAmount = parseFloat(debitSumAmount) + parseFloat(thisdebitAmount);
	    		}
	    		if(thiscreditAmount != ""){
	    			creditSumAmount = parseFloat(creditSumAmount) + parseFloat(thiscreditAmount);
	    		}
			}
	    	sumAmount = parseFloat(creditSumAmount)-parseFloat(debitSumAmount);
		   	$("#totalAmountLabel").text((sumAmount+"").replace("-",""));
		   	$("#totalAmount").val(sumAmount);
		   	formatMoneyText($("#totalAmountLabel"));
	    	if (sumAmount < 0) {
	    		$("#totalAmountLabel").prepend("-");
			}
    	}else {
    		$("#totalAmountLabel").text("");
		}
    	$.each($(".historyTr"), function(i, item){
    		$(item).find(".historyNo").text(i+1);
    		formatMoneyText($($(item).find(".debitAmountHis")));
    		formatMoneyText($($(item).find(".creditAmountHis")));
    	});
    }
    <!-- 保存创建/编辑信息 -->
    function saveForm(flag){
    	$("#activity_pane").showLoading();
    	$("select").removeAttr("disabled");
    	$("#hiddenSelect select").attr("disabled",true);
		$("textarea").removeAttr("disabled");
    	var params = $("#inputForm").serialize();
    	$.post("${ctx}/accounting/voucher/save?saveFlag="+flag,params,function(result){
    		/* 弹出提示框,刷新父页面,关闭当前页面 */
    		tipParent(result);
    		window.parent.frames[0].searchListForm();
    		closeWin();
    		$("#activity_pane").hideLoading();
    	},'json');
    	
    }
    <!-- 选择业务结算画面 -->
    function selectFinance(){
    	popup("900px","580px","${ctx}/accounting/voucher/selectFinance");
    	
    }
    <!-- 上传单据 -->
    function setUpload(uploadFileName,filePath) {
    	$("#superLink").attr("href",filePath+uploadFileName);
    	$("#superLink").html(uploadFileName);
    	$("#uploadFileName").val(uploadFileName);
		$("#filePath").val(filePath);
	}
    <!-- 删除行 -->
    function deleteCurrentRow(obj,financeId){
    	$(obj).parent().parent().parent().remove();
    	$(".hsty-"+financeId).remove();
    	sumAmount();
    }
    <!-- 变动会计科目删除样式 /设置文本域下拉框value值-->
    function checkNull(obj) {
    	$(obj).removeClass("select_red");
    	var trObj = $(obj).parent().parent().parent();
    	$(trObj).find(".accountItem").val(obj.value);
	}
    <!-- 设置文本域value值 -->
    function setValue(obj){
    	var trObj = $(obj).parent().parent().parent();
    	$(obj).text(obj.value);
    	$(trObj).find(".voucherRemarks").val(obj.value);
    }
    <!-- table入力 -->
    function onClickTd() {
    	$("#tableHtml").find("td[class^='tab-']").unbind("click"); 
	    $("#tableHtml").find("td[class^='tab-']").on("click",function(){
	    	 //找到当前鼠标单击的td
	        var tdObj = $(this);
	        unFormatText($(this));
            //保存原来的文本
            var oldText = $(this).text();
			var tdId = $(tdObj)[0].id;
	        //创建一个文本框
            var inputTemp;
            inputTemp = "<input type = 'text' value = '"+$(this).text()+"'  maxlength = '200'>";
	        var inputObj = $(inputTemp);
	        //去掉文本框的边框  
	        inputObj.css("border-width", 0);
	        inputObj.click(function () {
	            return false;
	        });
	        //使文本框的宽度和td的宽度相同  
	        inputObj.width(tdObj.width()-5);
	        inputObj.height(tdObj.height()-15);
	        //去掉文本框的外边距 
	        inputObj.css("margin", 0);
	        inputObj.css("padding", 0);
	        //把文本框放到td中  
	        tdObj.html(inputObj);
	        //文本框失去焦点的时候变为文本  
	        inputObj.blur(function () {
	            var newText = $(this).val();
                $(this).parent().parent().find("."+tdId).val(newText);
	            tdObj.html(newText);
	            formatMoneyText(tdObj, 2);
	            sumAmount();
	        });
	        //全选  
	        inputObj.trigger("focus").trigger("select");
	   });
	}
    //查存 
    function checkExist (t){
    	if ($(t).val() != "") {
    		$.post("${ctx}/accounting/voucher/checkExist?voucherNo="+$(t).val(),"",function(result){
        		/* 弹出提示框,刷新父页面,关闭当前页面 */
        		if (result == "true") {
			    	tipParent("编号已存在");
			    	$(t).addClass("error");
			    	existFlag = "false";
				}else{
					existFlag = "true";
				}
        	},'json');
		}
    }
    //
    function resetClass (t){
    	$(t).removeClass("error");
    }
    </script>
   <style type="text/css">
 	/* no  */
    .tab-no{width: 60px}
  	/* 款式图片  */
    .tab-kstp{width: 130px}
    .select_red{background-color: #fbaea8;}
</style>
</head>
<body>
<form id="inputForm">
<!-- 创建编辑头部 -->   
<div id="createHead" class="layerHeader clearfix">
<c:choose>
	   <c:when test="${voucherEntity.voucherId!=null && voucherEntity.voucherId!=''}">
	    <!-- 编辑 --> 
			<p class="headerTitle">编辑会计凭证</p>
       </c:when>
       <c:otherwise>
        <!-- 创建 --> 
            <p class="headerTitle">创建会计凭证</p>
       </c:otherwise>
    </c:choose>
    <div class="headerButton clearfix">
    	<button type="button" class="btn-js" onclick="popup('500px','324px','${ctx}/bigcargo/style/uploadFileMaterial?uploadFlg=1');">上传单据</button>
    	<button type="button" onclick="selectFinance();">选择业务结算</button>
        <button type="button" id="okButton"><spring:message code="purchaseContract_edit_ok"/></button>
        <button type="button" onclick="closeWin()"><spring:message code="purchaseContract_edit_cancle"/></button>
    </div>
</div>
<!-- 确认页面head部 -->
	<div id="okHead" class="layerHeader clearfix" style="display:none">
		<p class="headerTitle"><spring:message code="settlement_confirm"/></p>
	    <div class="headerButton clearfix">
		    	<shiro:hasPermission name="accounting:voucher:edit">
	        		<button type="button" onclick="saveForm(0);"><spring:message code="common_save"/></button>
	        		<button type="button" onclick="saveForm(1);">结算</button>
				</shiro:hasPermission>
	        <button type="button" onclick="closeWin()"><spring:message code="btn_cancle"/></button>
	    </div>
	</div>
<div id="showHtml" class="tabBoxSm">
<input type="hidden" id="voucherEntity.voucherId" name="voucherEntity.voucherId" value="${voucherEntity.voucherId }">
<div class="smOrderBox" id="activity_pane">                          
        <div class="clearfix">               	
            	<div class="items clearfix">        	
                    <div class="col-33 clearfix haveToBox">
	                    <!-- 凭证编号  -->
	                    <p class="tit">凭证编号</p> 
                        <input type="text" id="voucherNo" name="voucherEntity.voucherNo" value="${voucherEntity.voucherNo}" onfocus="resetClass(this);" onblur="checkExist(this);"/>              
                    	<p class="haveTo">*</p>
                    </div>
                    <div class="col-33 clearfix">
                    <!-- 日期 -->
                        <p class="tit">日期</p>
                        <c:choose>
	                        <c:when test="${voucherEntity.voucherId != null}">
	                        	<label id="financeNoLabel"><fmt:formatDate value="${voucherEntity.invoiceDate }" pattern="yyyy-MM-dd"/></label>
	                        	<input type="hidden" id="invoiceDate" name="voucherEntity.invoiceDate" value="${voucherEntity.invoiceDate }"/>
	                        </c:when>
	                        <c:otherwise>
		                        <label id="financeNoLabel">${fns:getDate("yyyy-MM-dd") }</label>
		                        <input type="hidden" id="invoiceDate" name="voucherEntity.invoiceDate" value="${fns:getDate('yyyy-MM-dd') }"/>
	                        </c:otherwise>
                        </c:choose>
                    </div>
                    <div class="col-33 clearfix">
                    <!-- 单据 -->
                        <p class="tit">单据</p>
                        <a id="superLink" href="${uploadFile.filePath }${uploadFile.uploadFile }">${uploadFile.uploadFile }</a>
	            		<input type="hidden" id="filePath" name="uploadFile.filePath" value="${uploadFile.filePath }">
	            		<input type="hidden" id="uploadFileName" name="uploadFile.uploadFile" value="${uploadFile.uploadFile }">
	            		<input type="hidden" id="fileId" name="uploadFile.fileId" value="${uploadFile.fileId }">
                    </div>
                </div>
                <div class="items clearfix">
                	<!-- 合计金额 -->
                    <p class="tit">合计金额</p> 
                    <label id="totalAmountLabel"><fmt:formatNumber value="${voucherEntity.totalAmount}" pattern="#,##0.00"/></label>
                    <input type="hidden" id="totalAmount" name="voucherEntity.totalAmount" value="${voucherEntity.totalAmount}"/> 
                </div>
            </div>
            <div class="listBg tabPadding overflowX">
		        <div class="tableArea">
		            <div class="tableScroll" style = "height:220px;width: 947px;overflow: auto;">
		                <table cellpadding="0" class="tableHead" style="z-index: 13;">
		                    <thead>
		                    <tr>
		                        <th><p class="tab-no">操作</p></th>
		                        <th><p class="width-100px">业务期间</p></th>
		                        <th><p class="width-120px">会计科目</p></th>
		                        <th><p class="width-100px">业务类别</p></th>
		                        <th><p class="width-120px">业务号</p></th>
		                        <th><p class="width-100px">货币类别</p></th>
		                        <th><p class="width-100px">借方</p></th>
		                        <th><p class="width-100px">贷方</p></th>
		                        <th><p class="width-120px">摘要</p></th>
		                    </tr>
		                    </thead>
		                </table>
		                <table cellpadding="0" class="tableBody" id="tableHtml">
		                    <tbody id="listHtml">
		                    <c:forEach items="${voucherDetailList }" var="voucherDetail">
                    			<tr>
						    		<td><p class="tab-no"><img src="${ctxStatic}/images/del_icon.png" style="width: 21px" onclick="deleteCurrentRow(this);"></p></td>
						    		<td><p class="width-100px"><fmt:formatDate value="${voucherDetail.financeEntity.financePeriod }" pattern="yyyy-MM"/></p></td>
						    		<td><p class="width-120px">
						    			<select style="width: 120px" onchange="checkNull(this)">
					                    	<option value="">请选择</option>
					                    	<c:forEach items="${financeItemList }" var="financeItem">
					                    		<option value="${financeItem.itemCode }"
					                    		<c:if test="${voucherDetail.accountItem eq financeItem.itemCode }">selected="selected"</c:if>
					                    		>${financeItem.itemName }</option>
					                    	</c:forEach>
					                    </select>
						    		</p></td>
						    		<td><p class="width-100px">${fns:getDictName(voucherDetail.financeEntity.financeCategory) }</p></td>
						    		<td><p class="width-120px">${voucherDetail.financeEntity.financeContractNo }</p></td>
						    		<td><p class="width-100px">${fns:getDictName(voucherDetail.financeEntity.financeCurrency) }</p></td>
						    		<td><p class="width-100px"><fmt:formatNumber value="${voucherDetail.debitAmount }" pattern="#,##0.00"/></p></td>
						    		<td><p class="width-100px"><fmt:formatNumber value="${voucherDetail.creditAmount }" pattern="#,##0.00"/></p></td>
						    		<td>
						    			<p class="width-120px">
						    				<textarea style="width:120px" onblur="setValue(this);">${voucherDetail.voucherRemarks }</textarea>
						    			</p>
						    		</td>
						    		<input type="hidden" class="voucherRemarks" name="voucherDetailEntity.voucherDetailList[tempIndex].voucherRemarks" value="${voucherDetail.voucherRemarks }"/>
						    		<input type="hidden" class="accountItem" name="voucherDetailEntity.voucherDetailList[tempIndex].accountItem" value="${voucherDetail.voucherRemarks }"/>
						    		<input type="hidden" class="settleAccountsId" name="voucherDetailEntity.voucherDetailList[tempIndex].settleAccountsId" value="${voucherDetail.settleAccountsId }"/>
						    		<input type="hidden" name="voucherDetailEntity.voucherDetailList[tempIndex].settleAccountsDate" value="${voucherDetail.settleAccountsDate }"/>
						    		<input type="hidden" name="voucherDetailEntity.voucherDetailList[tempIndex].settleAccountsStatus" value="${voucherDetail.settleAccountsStatus }"/>
						    		<input type="hidden" class="debitAmount" name="voucherDetailEntity.voucherDetailList[tempIndex].debitAmount" value="${voucherDetail.debitAmount }"/>
						    		<input type="hidden" class="creditAmount" name="voucherDetailEntity.voucherDetailList[tempIndex].creditAmount" value="${voucherDetail.creditAmount }"/>
						    	</tr>
                    		</c:forEach>
		                    </tbody>
		                </table>
		            </div>
		        </div>
		    </div>
		    <p style="font-size: 13px;left: 10px;position: relative;">凭证履历</p>
		    <div class="listBg tabPadding overflowX">
		        <div class="tableArea">
		            <div class="tableScroll" style = "height:220px;width: 947px;overflow: auto;">
		                <table cellpadding="0" class="tableHead" style="z-index: 13;">
		                    <thead>
		                    <tr>
		                    	<th><p class="tab-no">No.</p></th>
		                        <th><p class="width-100px">业务期间</p></th>
		                        <th><p class="width-120px">会计科目</p></th>
		                        <th><p class="width-100px">业务类别</p></th>
		                        <th><p class="width-120px">业务号</p></th>
		                        <th><p class="width-100px">货币类别</p></th>
		                        <th><p class="width-100px">借方</p></th>
		                        <th><p class="width-100px">贷方</p></th>
		                        <th><p class="width-120px">摘要</p></th>
		                    </tr>
		                    </thead>
		                </table>
		                <table cellpadding="0" class="tableBody" id="tableHtml">
		                    <tbody id="listHtmlHistory">
		                    </tbody>
		                </table>
		            </div>
		        </div>
		    </div>
        </div>                 
		    	
</form>
<div id="hiddenSelect" class="none">
	<select style="width: 120px" onchange="checkNull(this)">
		<option value="">请选择</option>
		<c:forEach items="${financeItemList }" var="financeItem">
			<option value="${financeItem.itemCode }">${financeItem.itemName }</option>
		</c:forEach>
	</select>
</div>
<script>
	window.onload = function(){
			var windowH = window.innerHeight
					||document.documentElement.clientHeight
					||document.body.clientHeight
			$('.tabSm').css({
				'height':windowH - 77,
			});
			$('.tableScrollSm').css({
				'height':windowH - 91
			})
		}
</script>
</body>
</html>
